// 2_ Prepare variables 2011
// //////////////////////////////////

// This do-file uses survey data to generate relevant variables for further analysis.

// ////////////////////////////////////////////////////////////////////////
// INDEX
// I.	Consumer price index, depending on month of interview
// II.	Labor income
// 	II.1. Labor income - primary occupation
// 	II.2. Labor income - secondary occupation
//  II.3. Total labor income 
// 	II.4. Income coming from dependent and independent activities	
// III.	Wage employment, Self-employment and Employment (defined by income of those activities)
// IV.	Weekly hours worked
// V. 	Demographics
// VI. 	Business Practices
// VII. Assets
// VIII.Labeling

// ////////////////////////////////////////////////////////////////////////



clear all
set more off

capture log close

log using "${path_logs}/2_Prepare variables 2011.log", replace

use "${path_data}/data2011.dta", clear

// ////////////////////////////////////////////////////////////////////////
// I. Consumer price index, depending on month of interview
// ////////////////////////////////////////////////////////////////////////

	gen ipc_nov2009=100.30
	gen ipc= 105.52 if date_month==9
	replace ipc= 106.03 if date_month ==10 | date_month==99 /*values 99 are codified to october (3 observations)*/
	replace ipc= 106.37 if date_month==11



// ////////////////////////////////////////////////////////////////////////	
// II. Labor income
// ////////////////////////////////////////////////////////////////////////

	// II.1. Labor income - primary occupation
			* Labor income - primary occupation - wage (monthly for wage earners according to d15)
			quiet gen ya1_sueldo=0 if ind_c2011==1
			quiet replace ya1_sueldo= ya1_sueldo + d15m if (d15m!=. & d15m!=88 & d15m!=99)
			quiet summ d15m if (d15m<=5000 & d15m!=88 & d15m!=99) , de 
			quiet gen rest=1 if (d15m==. | d15m==88 | d15m==99)  /*restriction for this variable*/
			replace ya1_sueldo =ya1_sueldo+ r(p50) if d15t==1 & rest==1  /*imputation of median*/		
			replace ya1_sueldo =ya1_sueldo+ 7500 if d15t==2 & rest==1
			replace ya1_sueldo=ya1_sueldo+ 12500 if d15t==3 & rest==1
			replace ya1_sueldo=ya1_sueldo+ 22500 if d15t==4 & rest==1
			replace ya1_sueldo= ya1_sueldo + 40000 if d15t==5 & rest==1
			replace ya1_sueldo=ya1_sueldo+ 75000 if d15t==6 & rest==1
			replace ya1_sueldo=ya1_sueldo+ 125000 if d15t==7 & rest==1
			replace ya1_sueldo= ya1_sueldo + 175000 if d15t==8 & rest==1
			replace ya1_sueldo= ya1_sueldo + 250000 if d15t==9 & rest==1
			replace ya1_sueldo= ya1_sueldo + 350000 if d15t==10 & rest==1
			replace ya1_sueldo=ya1_sueldo+ 450000 if d15t==11 & rest==1
			replace ya1_sueldo=ya1_sueldo+ 550000 if d15t==12 & rest==1
			replace ya1_sueldo=ya1_sueldo+ 650000 if d15t==13 & rest==1
			replace ya1_sueldo=ya1_sueldo+ 750000 if d15t==14 & rest==1
			replace ya1_sueldo=ya1_sueldo+ 850000 if d15t==15 & rest==1
			replace ya1_sueldo=ya1_sueldo+ 950000 if d15t==16 & rest==1
			replace ya1_sueldo=ya1_sueldo+ 1250000 if d15t==17 & rest==1
			replace ya1_sueldo=ya1_sueldo+ 2250000 if d15t==18 & rest==1
			summ d15m if (d15m>3000000 & d15m!=.) , de 
			replace ya1_sueldo=ya1_sueldo+ max(3000000,r(p50)) if d15t==19 & rest==1  /*imputation of median or CLP 3000000*/
			replace ya1_sueldo=. if rest==1 & (d15t==88 | d15t==99)
			replace ya1_sueldo=. if (d05==3 | d05==4 | d05==6 | d05==7 | d05==9) & d15m==. & d15t==. 
			replace ya1_sueldo=0 if d05==8 & ya1_sueldo==.
			drop rest
			
				
			* Labor income - primary occupation - business (para indepentents and employers, variable d16)
			quiet gen ya1_negocio=0 if ind_c2011==1
			quiet replace ya1_negocio=ya1_negocio + d16m if (d16m!=. & d16m!=88 & d16m!=99)
			quiet summ d16m if (d16m<=5000 & d16m!=88 & d16m!=99) , de 
			quiet gen rest=1 if (d16m==. | d16m==88 | d16m==99) /*restriction for this variable*/
			replace ya1_negocio =ya1_negocio+ r(p50) if d16t==1 & rest==1  /*imputation of median*/		
			replace ya1_negocio =ya1_negocio+ 7500 if d16t==2 & rest==1
			replace ya1_negocio=ya1_negocio + 12500 if d16t==3 & rest==1
			replace ya1_negocio=ya1_negocio + 22500 if d16t==4 & rest==1
			replace ya1_negocio=ya1_negocio + 40000 if d16t==5 & rest==1
			replace ya1_negocio=ya1_negocio + 75000 if d16t==6 & rest==1
			replace ya1_negocio=ya1_negocio + 125000 if d16t==7 & rest==1
			replace ya1_negocio=ya1_negocio + 175000 if d16t==8 & rest==1
			replace ya1_negocio= ya1_negocio + 250000 if d16t==9 & rest==1
			replace ya1_negocio= ya1_negocio + 350000 if d16t==10 & rest==1
			replace ya1_negocio=ya1_negocio+ 450000 if d16t==11 & rest==1
			replace ya1_negocio=ya1_negocio+ 550000 if d16t==12 & rest==1
			replace ya1_negocio=ya1_negocio+ 650000 if d16t==13 & rest==1
			replace ya1_negocio=ya1_negocio+ 750000 if d16t==14 & rest==1
			replace ya1_negocio=ya1_negocio+ 850000 if d16t==15 & rest==1
			replace ya1_negocio=ya1_negocio+ 950000 if d16t==16 & rest==1
			replace ya1_negocio=ya1_negocio+ 1250000 if d16t==17 & rest==1
			replace ya1_negocio=ya1_negocio+ 2250000 if d16t==18 & rest==1
			summ d16m if (d16m>3000000 & d16m!=.) , de /*no observations*/
			replace ya1_negocio=ya1_negocio+ max(3000000,r(p50)) if d16t==19 & rest==1  /*imputation of median or CLP 3000000*/
			replace ya1_negocio=. if rest==1 & (d16t==88 | d16t==99)
			drop rest
			replace ya1_negocio=. if (d05==1 | d05==2) & d16m==. & d16t==.
				
			* Labor income - primary occupation - business products withdrew for personal expenses (for indepentents and employers variable d17)
			quiet gen ya1_productos=0 if ind_c2011==1
			replace ya1_productos=ya1_productos + d17m if (d17m!=. & d17m!=88 & d17m!=99)
			quiet summ d17m if (d17m<5000 & d17m!=88 & d17m!=99) , de
			qui gen rest = 1 if (d17m==. | d17m==88 | d17m==99)
			replace ya1_productos =ya1_productos+ r(p50) if d17t==1 & rest==1  /*imputation of median*/		
			replace ya1_productos =ya1_productos + 7500 if d17t==2 & rest==1
			replace ya1_productos =ya1_productos + 12500 if d17t==3 & rest==1
			replace ya1_productos=ya1_productos + 22500 if d17t==4 & rest==1
			replace ya1_productos=ya1_productos + 40000 if d17t==5 & rest==1
			replace ya1_productos=ya1_productos + 75000 if d17t==6 & rest==1
			replace ya1_productos=ya1_productos + 125000 if d17t==7 & rest==1
			replace ya1_productos=ya1_productos + 175000 if d17t==8 & rest==1
			replace ya1_productos= ya1_productos + 250000 if d17t==9 & rest==1
			replace ya1_productos=ya1_productos + 350000 if d17t==10 & rest==1
			replace ya1_productos=ya1_productos+ 450000 if d17t==11 & rest==1
			replace ya1_productos=ya1_productos+ 550000 if d17t==12 & rest==1
			replace ya1_productos=ya1_productos+ 650000 if d17t==13 & rest==1
			replace ya1_productos=ya1_productos+ 750000 if d17t==14 & rest==1
			replace ya1_productos=ya1_productos+ 850000 if d17t==15 & rest==1
			replace ya1_productos=ya1_productos+ 950000 if d17t==16 & rest==1
			replace ya1_productos=ya1_productos+ 1250000 if d17t==17 & rest==1
			replace ya1_productos=ya1_productos+ 2250000 if d17t==18 & rest==1
			quiet summ d17m if (d17m>3000000 & d17m!=.) , de 
			quiet replace ya1_productos=ya1_productos+ max(3000000,r(p50)) if d17t==19 & rest==1  /*imputation of median or CLP 3000000*/
			replace ya1_productos=. if rest==1 & (d17t==88 | d17t==99)
			drop rest
			replace ya1_productos=. if (d05==1 | d05==2) & d17m==. & d17t==. 
			
			
	// II.2. Labor income - secondary occupation
			* Labor income - secondary occupation - wage (monthly for wage earners according to d28)
			quiet gen ya2_sueldo=0 if ind_c2011==1
			quiet replace ya2_sueldo= ya2_sueldo + d28m if (d28m!=. & d28m!=88 & d28m!=99)
			quiet summ d28m if (d28m<5000 & d28m!=88 & d28m!=99) , de
			qui gen rest = 1 if (d28m==. | d28m==88 | d28m==99)
			quiet replace ya2_sueldo =ya2_sueldo+ r(p50) if d28t==1 & rest==1  /*imputation of median*/		
			quiet replace ya2_sueldo =ya2_sueldo + 7500 if d28t==2 & rest==1
			quiet replace ya2_sueldo =ya2_sueldo + 12500 if d28t==3 & rest==1
			quiet replace ya2_sueldo=ya2_sueldo + 22500 if d28t==4 & rest==1
			quiet replace ya2_sueldo=ya2_sueldo + 40000 if d28t==5 & rest==1
			quiet replace ya2_sueldo=ya2_sueldo + 75000 if d28t==6 & rest==1
			quiet replace ya2_sueldo=ya2_sueldo + 125000 if d28t==7 & rest==1
			quiet replace ya2_sueldo=ya2_sueldo + 175000 if d28t==8 & rest==1
			quiet replace ya2_sueldo= ya2_sueldo + 250000 if d28t==9 & rest==1
			quiet replace ya2_sueldo=ya2_sueldo + 350000 if d28t==10 & rest==1
			quiet replace ya2_sueldo=ya2_sueldo+ 450000 if d28t==11 & rest==1
			quiet replace ya2_sueldo=ya2_sueldo+ 550000 if d28t==12 & rest==1
			quiet replace ya2_sueldo=ya2_sueldo+ 650000 if d28t==13 & rest==1
			quiet replace ya2_sueldo=ya2_sueldo+ 750000 if d28t==14 & rest==1
			quiet replace ya2_sueldo=ya2_sueldo+ 850000 if d28t==15 & rest==1
			quiet replace ya2_sueldo=ya2_sueldo+ 950000 if d28t==16 & rest==1
			quiet replace ya2_sueldo=ya2_sueldo+ 1250000 if d28t==17 & rest==1
			quiet replace ya2_sueldo=ya2_sueldo+ 2250000 if d28t==18 & rest==1
			quiet summ d28m if (d28m>3000000 & d28m!=.) , de 
			quiet replace ya2_sueldo=ya2_sueldo+ max(3000000,r(p50)) if d28t==19 & rest==1  /*imputation of median or CLP 3000000*/
			replace ya2_sueldo=. if rest==1 & (d28t==88 | d28t==99)
			replace ya2_sueldo=. if (d18==3 | d18==4 | d18==5 | d18==6 | d18==7 | d18==9 ) & d28m==. & d28t==.
			replace ya2_sueldo=0 if d18==8 & ya2_sueldo==.
			drop rest
			
			* Labor income - secondary occupation - business (para indepentents and employers, variable d29)
			quiet gen ya2_negocio=0 if ind_c2011==1
			quiet summ d29m if (d18==1 | d18==2) , de
			quiet replace ya2_negocio = ya2_negocio + d29m if (d29m!=. & d29m!=88 & d29m!=99)
			quiet summ d29m if (d29m<5000 & d29m!=88 & d29m!=99) , de
			quiet gen rest=1 if (d29m==. | d29m==88 | d29m==99)
			quiet replace ya2_negocio =ya2_negocio+ r(p50) if d29t==1 & rest==1  /*imputation of median*/		
			quiet replace ya2_negocio =ya2_negocio + 7500 if d29t==2 & rest==1
			quiet replace ya2_negocio =ya2_negocio + 12500 if d29t==3 & rest==1
			quiet replace ya2_negocio=ya2_negocio + 22500 if d29t==4 & rest==1
			quiet replace ya2_negocio=ya2_negocio + 40000 if d29t==5 & rest==1
			quiet replace ya2_negocio=ya2_negocio + 75000 if d29t==6 & rest==1
			quiet replace ya2_negocio=ya2_negocio + 125000 if d29t==7 & rest==1
			quiet replace ya2_negocio=ya2_negocio + 175000 if d29t==8 & rest==1
			quiet replace ya2_negocio= ya2_negocio + 250000 if d29t==9 & rest==1
			quiet replace ya2_negocio=ya2_negocio + 350000 if d29t==10 & rest==1
			quiet replace ya2_negocio=ya2_negocio+ 450000 if d29t==11 & rest==1
			quiet replace ya2_negocio=ya2_negocio+ 550000 if d29t==12 & rest==1
			quiet replace ya2_negocio=ya2_negocio+ 650000 if d29t==13 & rest==1
			quiet replace ya2_negocio=ya2_negocio+ 750000 if d29t==14 & rest==1
			quiet replace ya2_negocio=ya2_negocio+ 850000 if d29t==15 & rest==1
			quiet replace ya2_negocio=ya2_negocio+ 950000 if d29t==16 & rest==1
			quiet replace ya2_negocio=ya2_negocio+ 1250000 if d29t==17 & rest==1
			quiet replace ya2_negocio=ya2_negocio+ 2250000 if d29t==18 & rest==1
			quiet summ d29m if (d29m>3000000 & d29m!=.) , de 
			quiet replace ya2_negocio=ya2_negocio+ max(3000000,r(p50)) if d29t==19 & rest==1  /*imputation of median or CLP 3000000*/
			replace ya2_negocio=. if rest==1 & (d29t==88 | d29t==99)
			replace ya2_negocio=. if (d18==1 | d18==2) & d29m==. & d29t==.
			drop rest
			
			* Labor income - secondary occupation - business products withdrew for personal expenses (for indepentents and employers variable d30)
			quiet gen ya2_productos=0 if ind_c2011==1
			quiet replace ya2_productos = ya2_productos + d30m if (d30m!=. & d30m!=88 & d30m!=99)
			quiet summ d30m if (d30m<5000 & d30m!=88 & d30m!=99) , de
			quiet gen rest=1 if (d30m==. | d30m==88 | d30m==99)
			quiet replace ya2_productos =ya2_productos+ r(p50) if d30t==1 & rest==1  /*imputation of median*/		
			quiet replace ya2_productos =ya2_productos + 7500 if d30t==2 & rest==1
			quiet replace ya2_productos =ya2_productos + 12500 if d30t==3 & rest==1
			quiet replace ya2_productos=ya2_productos + 22500 if d30t==4 & rest==1
			quiet replace ya2_productos=ya2_productos + 40000 if d30t==5 & rest==1
			quiet replace ya2_productos=ya2_productos + 75000 if d30t==6 & rest==1
			quiet replace ya2_productos=ya2_productos + 125000 if d30t==7 & rest==1
			quiet replace ya2_productos=ya2_productos + 175000 if d30t==8 & rest==1
			quiet replace ya2_productos=ya2_productos + 250000 if d30t==9 & rest==1
			quiet replace ya2_productos=ya2_productos + 350000 if d30t==10 & rest==1
			quiet replace ya2_productos=ya2_productos+ 450000 if d30t==11 & rest==1
			quiet replace ya2_productos=ya2_productos+ 550000 if d30t==12 & rest==1
			quiet replace ya2_productos=ya2_productos+ 650000 if d30t==13 & rest==1
			quiet replace ya2_productos=ya2_productos+ 750000 if d30t==14 & rest==1
			quiet replace ya2_productos=ya2_productos+ 850000 if d30t==15 & rest==1
			quiet replace ya2_productos=ya2_productos+ 950000 if d30t==16 & rest==1
			quiet replace ya2_productos=ya2_productos+ 1250000 if d30t==17 & rest==1
			quiet replace ya2_productos=ya2_productos+ 2250000 if d30t==18 & rest==1
			quiet summ d30m if (d30m>3000000 & d30m!=.) , de 
			quiet replace ya2_productos=ya2_productos+ max(3000000,r(p50)) if d30t==19 & rest==1  /*imputation of median or CLP 3000000*/
			replace ya2_productos=. if rest==1 & (d30t==88 | d30t==99)
			drop rest
			replace ya2_productos=. if (d18==1 | d18==2) & d30m==. & d30t==.
			 
		** Imputation of missing values for observations with no information on work variables (d01, d02, d03)
		foreach x in ya1_sueldo ya1_negocio ya1_productos ya2_sueldo ya2_negocio ya2_productos {
			replace `x' = . if ((d01!=1 & d01!=2) & (d02!=1 & d02!=2) & (d03!=1 & d03!=2)) ///
								| ((d01==2) & (d02!=1 & d02!=2) & (d03!=1 & d03!=2)) ///
								| ((d01==2) & (d02==2) & (d03!=1 & d03!=2)) 
		}					
		
		
		** Grouping of income coming from primary and secondary occupation
		for any 1 2 : egen yaX=  rsum(yaX_sueldo yaX_negocio yaX_productos) if yaX_sueldo!=. | yaX_negocio!=. | yaX_productos!=.
		
	
	// II.3. Total Labor Income
	
		egen ylaborales = rsum(ya1 ya2) if ya1!=. | ya2!=.
		replace ylaborales=. if ya1_negocio==. &  ya1_productos==. & (d18==10 | d18==99)
		replace ylaborales=. if ya1_negocio==. &  ya1_productos==. & ya2_negocio==. & ya2_productos==.
		replace ylaborales=. if ya1_sueldo==. & (d18==10 | d18==99)
		replace ylaborales=. if ya1_sueldo==. & ya2_sueldo==.


	// II.4. Income coming from dependent and independent activities

		egen ydeptes= rsum(ya1_sueldo ya2_sueldo) if (ya1_sueldo!=. | ya2_sueldo!=.)
		replace ydeptes=. if ya1_sueldo==. & (d18==10 | d18==99)
		replace ydeptes=. if ya1_sueldo==. & ya2_sueldo==.
		 
		egen yindeptes= rsum(ya1_negocio  ya1_productos ya2_negocio  ya2_productos ) if ya1_negocio!=. | ya1_productos!=. | ya2_negocio!=. |  ya2_productos!=.
		replace yindeptes=. if ya1_negocio==. & ya1_productos==. & (d18==10 | d18==99)
		replace yindeptes=. if ya1_negocio==. & ya1_productos==. & ya2_negocio==. & ya2_productos==.
		
							
	
	
	
	** Conversion of monetary variables to November 2009 chilean pesos
	foreach y in ya1_sueldo ya1_negocio ya1_productos ya2_sueldo ya2_negocio ya2_productos ya1 ya2 ylaborales ydeptes yindeptes {
		replace `y'=`y'*(ipc_nov2009/ipc)
	}
	
	
// ////////////////////////////////////////////////////////////////////////		
// III. Wage employment, Self-employment and Employment (defined by income of those activities)
// ////////////////////////////////////////////////////////////////////////	
	
	gen dependiente = (ydeptes>0) if ind_c2011==1 & ydeptes!=. & yindeptes!=.
	gen independiente = (yindeptes>0) if ind_c2011==1 & ydeptes!=. & yindeptes!=.
	gen trabaja=(ylaborales>0) if ylaborales!=.
	
	
// ////////////////////////////////////////////////////////////////////////		
// IV. Weekly hours worked
// ////////////////////////////////////////////////////////////////////////	
	gen ht=0 if ind_c2011==1
	replace ht=d06 					if d06!=88 & d06!=99 & d06!=. & ind_c2011==1
	replace ht=ht+d19 if d19!=88 & d19!=99 & d19!=. & ind_c2011==1
    replace ht=.							if (d06==88 | d06==99) & (d19==88 | d19==99 | d19==.)

	
		
// ////////////////////////////////////////////////////////////////////////	
// V. Demographics
// ////////////////////////////////////////////////////////////////////////	

	** Years of education
	quiet gen esc= 0 if (b05t<=2 | b05t==7)
	quiet replace esc=b05c if (b05t==3 | b05t==5) & (b05c!=88 & b05c!=99 & b05c!=.)
	quiet replace esc=6 if b05t==4
	quiet replace esc=8 if b05t==6
	quiet replace esc=6 + b05c if (b05t==8 | b05t==12) & (b05c!=88 & b05c!=99 & b05c!=.)
	quiet replace esc=12 if b05t==9 | b05t==11 | b05t==13 | b05t==15
	quiet replace esc=8 + b05c if (b05t==10 | b05t==14) & (b05c!=88 & b05c!=99 & b05c!=.)
	quiet replace esc=12 + b05c if (b05t==16 | b05t==17 | b05t==18) & (b05c!=88 & b05c!=99 & b05c!=.)
	quiet replace esc=17 + b05c if b05t==19 & (b05c!=88 & b05c!=99 & b05c!=.)
	

	** Educational level
	gen basica= (b05t<=7) if (b05t!=88 & b05t!=99 & b05t!=.)
	gen media_inc= (b05t==8 | b05t==10 | b05t==12 | b05t==14) if (b05t!=88 & b05t!=99 & b05t!=.)
	gen media= (b05t==9 | b05t==11 | b05t==13 | b05t==15) if (b05t!=88 & b05t!=99 & b05t!=.)
	gen superior= (b05t>=16 & b05t<=19) if (b05t!=88 & b05t!=99 & b05t!=.)

	
	** Age
	gen edad =  a03 
	
	
	** Gender (1 male, 0 female) 
	generate sexo= a02
	recode sexo (2 = 0)
	label define sexolbl 1 "Male" 0 "Female"
	label values sexo sexolbl
	label var sexo "Dummy Gender (1=Male)"
	
		
	
// ////////////////////////////////////////////////////////////////////////	
// VI. Business practices
// ////////////////////////////////////////////////////////////////////////	
	
** Marketing

local i=1
foreach item in 01a 01b 01c 01d 01e 02 03 04 07 {

gen prac_m`i'=0 if ind_c2011==1
replace prac_m`i'=1 if m`item'==1
replace prac_m`i'=. if (m`item'==88 | m`item'==99)

local i=`i'+1
}

egen marketing=rsum(prac_m1 prac_m2 prac_m3 prac_m4 prac_m5 prac_m6 prac_m7 prac_m8 prac_m9) if ind_c2011==1
replace marketing=. if (prac_m1==. & prac_m2==. & prac_m3==. & prac_m4==. & prac_m5==. & prac_m6==. & prac_m7==. & prac_m8==. & prac_m9==.)


* Inventory management

local i=1
foreach item in 08 09 10 15 17 {

gen prac_s`i'=0 if ind_c2011==1
replace prac_s`i'=1 if m`item'==1
replace prac_s`i'=. if (m`item'==88 | m`item'==99)

local i=`i'+1
}

egen stock=rsum(prac_s1 prac_s2 prac_s3 prac_s4 prac_s5 ) if ind_c2011==1
replace stock=. if (prac_s1==. & prac_s2==. & prac_s3==. & prac_s4==. & prac_s5==.)


* Costing and record keeping

local i=1 

foreach item in 18 19 20 21 22 23 25 {

gen prac_c`i'=0 if ind_c2011==1
replace prac_c`i'=1 if m`item'==1
replace prac_c`i'=. if (m`item'==88 | m`item'==99)

local i=`i'+1

}

egen cost=rsum(prac_c1 prac_c2 prac_c3 prac_c4 prac_c5  prac_c6 prac_c7) if ind_c2011==1
replace cost=. if (prac_c1==. & prac_c2==. & prac_c3==. & prac_c4==. & prac_c5==. & prac_c6==. & prac_c7==.)

* Financial planning

local i=1 
foreach item in 27 28  {
gen prac_p`i'=0 if ind_c2011==1
replace prac_p`i'=1 if m`item'==1
replace prac_p`i'=. if (m`item'==88 | m`item'==99)

local i=`i'+1
}

gen prac_p3=1 if m26==4  
replace prac_p3=. if ( m26==88 | m26==99) 
 
gen prac_p4=1 if (m29a==1 | m29b==1 | m29c==1 | m29d==1 ) 
replace prac_p4=. if (m29a==. & m29b==. & m29c==. & m29d==.) 

egen plan=rsum(prac_p1 prac_p2 prac_p3 prac_p4 ) 
replace plan=. if (prac_p1==. & prac_p2==. & prac_p3==. & prac_p4==. ) 


*** Business practices

egen tot_practics=rsum(marketing stock cost plan) if ind_c2011==1
replace tot_practics=. if (marketing==. & stock==. & cost==. & plan==. ) 

*** Registry book available

gen ver_registro=1 if m34==1
recode ver_registro .=0
replace ver_registro=. if (m34==88 | m34==99)

*** Inventory available

gen ver_inven=1 if m35==1
recode ver_inven .=0
replace ver_inven=. if (m35==88 | m35==99)

** Available cash (CLP)
gen dinero_mano= m30
recode dinero_mano .=0
replace dinero_mano=. if (m30==88 | m30==99)


*** Correction of response rates
*** Variable that identifies self-employment or employers (primary or secondary occupation)
gen cta_prop=1 if d05==1 | d05==2
replace cta_prop=1 if d18==1 | d18==2
recode cta_prop .=0

*** Variable that identifies who answers Modules G and M of the questionnaire
gen emprendedor=1 if g01==1
recode emprendedor .=0

*** Indicator if responder in Module G, but no independent worker
gen no_ind_si_emprende=1 if cta_prop==0 & emprendedor==1
recode no_ind_si_emprende .=0

*** Indicator if not responder in Module G, but independent worker

gen si_ind_no_emprende=1 if cta_prop==1 & emprendedor==0
recode si_ind_no_emprende .=0

*** Correction of response rates in business practices

replace marketing =0 if no_ind_si_emprende==1 & marketing!=. & ind_c2011==1
replace stock =0 if no_ind_si_emprende==1 & stock!=.  & ind_c2011==1
replace cost =0 if no_ind_si_emprende==1 & cost!=. & ind_c2011==1
replace plan=0 if no_ind_si_emprende==1 & plan!=. & ind_c2011==1
replace tot_practics=0 if no_ind_si_emprende==1 & tot_practics!=. & ind_c2011==1
replace ver_registro=0 if no_ind_si_emprende==1 & ver_registro!=. & ind_c2011==1
replace ver_inven=0 if no_ind_si_emprende==1 & ver_inven!=. & ind_c2011==1
replace dinero_mano=0 if no_ind_si_emprende==1 & dinero_mano!=. & ind_c2011==1






// ////////////////////////////////////////////////////////////////////////	
// VII. Labeling
// ////////////////////////////////////////////////////////////////////////	

label var marketing "Marketing Index"
label var stock "Inventory management"
label var cost "Costing and record keeping"
label var plan "Financial Planning"
label var ver_registro "Registry book available"
label var ver_inven "Inventory available"
label var dinero_mano "Available Cash"
label var dependiente "Wage employment"
label var independiente "Self-employment"
label var trabaja "Total employment"
label var ht "Weekly hours worked"
label var ylaborales "Total labor income"
label var ydeptes "Wage employment income"
label var yindeptes "Self employment income"
label var basica "Primary education"
label var media_inc "Secondary education incomplete"
label var media "Secondary educaction complete"
label var superior "Tertiary education"
label var esc "Years of education"



***********************************************************************************************************	
*** Saving database

	keep id id2010 orden adm_codigo_p ind_c2011 mesp mesp_p C anymesp TT sexo edad esc basica media_inc media superior ydeptes yindeptes ylaborales independiente dependiente trabaja marketing stock cost plan tot_pr ver_registro ver_inven dinero_mano ht fichaps
	
	
	sort id orden
	bysort id : keep if _n== 1


	drop orden
	sort id
	
	save "${path_data}/variables2011", replace
	
	
log close
	
